package com.ocpap.rest.controller;

import com.alibaba.fastjson.JSONObject;
import com.ocpap.rest.entity.Goods;
import com.ocpap.rest.entity.SysUser;
import com.ocpap.rest.service.WordService;
import com.ocpap.rest.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private WordService wordService;

    @RequestMapping("index")
    public String index(SysUser sysUser, Goods goods,Model model, HttpSession session){
        List<SysUser> sysUsers = sysUserService.findAll(sysUser);
        List<Goods> goods1 = wordService.findAll(goods);
        if(sysUsers!=null&&sysUsers.size()>0){
            model.addAttribute("sys",sysUsers.size());
        }
        if(goods1!=null&&goods1.size()>0){
            model.addAttribute("goods1",goods1.size());
        }
        return "index";
    }

    @RequestMapping("login")
    public String login(){
        return "/login";
    }

    @RequestMapping("resign")
    public String resign(){
        return "/resign";
    }

    @RequestMapping("loginuser")
    @ResponseBody
    public JSONObject json(SysUser sysuser, HttpSession session){

        JSONObject json = new JSONObject();
        SysUser sysUser = sysUserService.findByPassAndAccount(sysuser);
        session.setAttribute("user",sysUser);
        if(sysUser!=null){
            json.put("status","succ");
            json.put("msg","登陆成功");
            json.put("flag",sysUser.getIsSystem());
        }else{
            json.put("status","error");
            json.put("msg","登陆失败");
        }
        return json;
    }

    @RequestMapping("list")
    public String list(SysUser sysUser,Model model){

        List<SysUser> sysUserList = sysUserService.findAll(sysUser);
        model.addAttribute("sysUserList",sysUserList);
        return "/userlist";
    }

    @RequestMapping("add")
    public String add(SysUser sysUser, Model model){
        return "/useradd";
    }

    @RequestMapping("update")
    public String update(Integer id,Model model){

        SysUser sysUser = sysUserService.findById(id);
        model.addAttribute("sysUser",sysUser);
        return "userupdate";
    }

    @RequestMapping("insert")
    @ResponseBody
    public JSONObject insert(SysUser sysUser){

        JSONObject json = new JSONObject();
        int result = sysUserService.insert(sysUser);
        if(result>0){
            json.put("status","succ");
            json.put("msg","添加成功");
        }else{
            json.put("status","error");
            json.put("msg","添加失败");
        }
        return json;
    }

    @RequestMapping("mod")
    @ResponseBody
    public JSONObject mod(SysUser sysUser){

        JSONObject json = new JSONObject();
        int result = sysUserService.update(sysUser);
        if(result>0){
            json.put("status","succ");
            json.put("msg","更新成功");
        }else{
            json.put("status","error");
            json.put("msg","更新失败");
        }
        return json;
    }
}
